import { FilterSpecification } from 'mapbox-gl';
import { EngiGis } from '..';

import { IconLayer } from '../map/layers/IconLayer';
import { IBizUnitConfig, ModelObjType } from '../shared/types';
import logger from 'loglevel';
import { ModelBizUnit } from './modelBizUnit';

export class ValveBizUnit extends ModelBizUnit {
  objType: ModelObjType = 'valve'; // 业务单元类型
  unitType: string = this.objType;
  constructor(engiGis: EngiGis) {
    super(engiGis);
  }

  filter(conditions: FilterSpecification) {
    this.layers.forEach((layer) => {
      layer.filter(conditions);
    });
  }

  async setRenderer(config: IBizUnitConfig): Promise<IconLayer[]> {
    logger.info('开始绘制阀门图层');
    const engiGis = this.engiGis;
    const icons = {
      'valve-0-gray': 'public/icons/valve-0-gray.png',
    };
    await engiGis.mapCore.loadImages(icons);
    const iconLayer = new IconLayer(
      {
        layerId: this.layerId,
        sourceId: this.sourceId,
        map: this.map,
        sort: config.sort || 0,
      },
      {
        layout: {
          ...this.getLayoutBase(),
          'icon-image': 'valve-0-gray',
          'symbol-sort-key': config.sort || 0,
          'icon-allow-overlap': true,
          'icon-ignore-placement': true,
        },
      },
    );
    return [iconLayer];
  }
}
